package com.bis.ymyy.controller;

import com.bis.ymyy.common.utils.AddUserInfo;
import com.bis.ymyy.common.utils.QueryPage;
import com.bis.ymyy.common.utils.Result;
import com.bis.ymyy.entity.Doctor;
import com.bis.ymyy.service.DoctorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/doctor")
@Api(tags = "/doctor相关接口")
public class DoctorController {

    @Autowired(required = false)
    private DoctorService doctorService;

    @ApiOperation("/分页查询doctor")
    @PostMapping("/queryPageUser")
    public Result<Object> queryPageUser(@RequestBody(required = false) QueryPage page){
        Result<Object> result = new Result<>();
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            List<Doctor> models = doctorService.getAll();
            PageInfo<Doctor> pageInfo = new PageInfo<>(models);
            result.setCode(200);
            result.setSuccess(true);
            result.setMsg("success");
            result.setData(pageInfo);
        } else {
            result.setData(doctorService.getAll());
            result.setMsg("success");
            result.setSuccess(true);
        }
        return result;
    }

    @ApiOperation("/根据id查询")
    @GetMapping("/findDoctorById")
    public Result<Doctor> findDoctorById(@RequestParam(name = "userId",required = true) int userId){

        Result<Doctor> result = new Result<>();
        Doctor doctor = doctorService.findById(userId);
        result.setData(doctor);
        result.setCode(200);
        result.setMsg("查询成功");
        if (doctor == null){
            result.setData(null);
            result.setCode(400);
            result.setMsg("用户不存在");
        }
        return result;
    }

    @ApiOperation("/更新doctor信息")
    @PostMapping(value = "/updateDoctor", produces = "application/json;charset=UTF-8")
    public Result updateDoctor(@RequestBody Doctor doctor){
        Result result = new Result();
        if (doctorService.modifyById(doctor) == 0){
            result.setMsg("未修改任何数据");
            result.setSuccess(false);
            result.setCode(400);
        } else {
            result.setSuccess(true);
            result.setMsg("更新成功");
            result.setCode(200);
        }
        return result;
    }

    @ApiOperation("/删除doctor")
    @PostMapping("/deleteDoctorById")
    public Result deleteDoctorById(@RequestBody Map params){
        Result result = new Result();
        result.setCode(200);
        result.setMsg("删除成功");
        result.setSuccess(true);
        String[] idsArr = params.get("ids").toString().split(",");
        for (String s : idsArr) {
            doctorService.deleteById(Integer.parseInt(s));
        }
        return result;
    }

    @PostMapping("/addDoctor")
    public Result addDoctor(@RequestBody AddUserInfo userInfo){
        Doctor doctor = doctorService.findByUserName(userInfo.getUserName());
        Result result = new Result();
        if (doctor != null){
            result.setCode(400);
            result.setSuccess(false);
            result.setMsg("用户名已存在，添加失败");
        } else if(doctorService.addBaseDoctor(userInfo) != 0) {
            result.setMsg("添加成功");
            result.setCode(200);
            result.setSuccess(true);
            result.setData(doctorService.findByUserName(userInfo.getUserName()));
        }

        return result;
    }

}
